package com.zhangkeyou.map.mapweb.util;

import com.google.common.base.Strings;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MyUtils {
    public static final String USER_INFO_TOKEN = "appLoginToken";
    public static final String ARTICLE_ID_TOKEN = "articleIdToken";
    public static final String TOKEN_NAME_IN_REDIS_PRE = "userId:";
    public static final String TOKEN_NAME_IN_HEADER = "token";


    public static String getMD5(String pwd){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");// 生成一个MD5加密计算摘要
            //
            md.update(pwd.getBytes());// 计算md5函数
            // /**         * digest()最后确定返回md5 hash值，返回值为8位字符串。         * 因为md5 hash值是16位的hex值，实际上就是8位的字符         * BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值         * 一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）         */
            String hashedPwd = new BigInteger(1, md.digest()).toString(16);// 16是表示转换为16进制数
            return hashedPwd;
        }catch(NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] File2ByteArray(File file){
        try{
            FileInputStream fis = new FileInputStream(file);
            System.out.println("bytes of file: "+fis.available());
            byte[] data=new byte[fis.available()];
            fis.read(data,0,fis.available());
            fis.close();
            return data;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 获取IP地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String xIp = request.getHeader("X-Real-IP");
        String xFor = request.getHeader("X-Forwarded-For");

        if (!Strings.isNullOrEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = xFor.indexOf(",");
            if (index != -1) {
                return xFor.substring(0, index);
            } else {
                return xFor;
            }
        }
        xFor = xIp;
        if (!Strings.isNullOrEmpty(xFor) && !"unKnown".equalsIgnoreCase(xFor)) {
            return xFor;
        }
        if (Strings.nullToEmpty(xFor).trim().isEmpty() || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("Proxy-Client-IP");
        }
        if (Strings.nullToEmpty(xFor).trim().isEmpty() || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (Strings.nullToEmpty(xFor).trim().isEmpty() || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (Strings.nullToEmpty(xFor).trim().isEmpty() || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (Strings.nullToEmpty(xFor).trim().isEmpty() || "unknown".equalsIgnoreCase(xFor)) {
            xFor = request.getRemoteAddr();
        }


        return "0:0:0:0:0:0:0:1".equals(xFor) ? "127.0.0.1" : xFor;
    }

    public static enum FileSource{
        ARTICLE,FILE
    }

    public static enum FileArticleSave{
        SAVE,NO_SAVE
    }
}
